<template>
  <div class="PieChart">
    <div v-if="chartData && chartData.length" class="chart-box">
      <!-- <div class="bg-border"></div> -->
      <div class="title">
        <div class="text">总数</div>
        <div class="num">
          <span>{{total}}个</span>
        </div>
      </div>
      <div ref="chartRef" class="chart"></div>
    </div>
    <div v-else class="s-empty">
      <!-- <img src="../../../assets/images/views/board/noData.png"  alt=""/> -->
    </div>
  </div>
</template>
<script setup lang="ts" name="secureChat">
import { onMounted, defineProps, watch, nextTick, ref, onUnmounted, defineEmits } from "vue";
import { nowSize } from "@/utils/utils";
import * as echarts from "echarts";
const props = defineProps({
  chartData: {
    type: Array,
    default: () => [],
  },
  color: {
    type: Array,
    default: () => ["#31bef3", "#235aff", "#f56b6b", "#3fffaa", "#ffeb69"],
  },
  total: {
    type: Number,
    default: () => 0,
  },
});
const emit = defineEmits(["chartClick"]);
const chartRef: any = ref(null);
let myChart: any;

// 初始化echarts图表
const initChart = () => {
  if (!myChart) {
    myChart = echarts.init(chartRef.value);
    myChart.on("click", e => {
      emit("chartClick", e.data.name);
    });
  }
  let option: any = {
    tooltip: {
      trigger: "item",
      borderWidth: nowSize(2), // 设置边框宽度
      backgroundColor: "rgba(7,31,52,0.8)",
      textStyle: {
        color: "#BBD0DC",
      },
    },
    // title: {
    //   text: "总数\n215个",
    //   //   left: '40%',
    //   left: "center",
    //   y: "center",
    //   textStyle: {
    //     fontSize: nowSize(46),
    //     lineHeight: nowSize(58),
    //     color: "rgba(85, 255, 255, 0.99)",
    //   },
    // },
    legend: {
      top: "bottom",
      left: "5%",
      icon: "rect",
      itemGap: nowSize(40),
      itemWidth: nowSize(40), // 设置图例标记的宽度为正方形
      itemHeight: nowSize(12), // 设置图例标记的高度为正方形
      textStyle: {
        padding: [0, nowSize(5), 0, nowSize(5)],
        rich: {
          name: {
            fontSize: nowSize(40), // 可以根据需要调整大小
            color: "#C0CEE5",
          },
          value: {
            color: "#00FFFF",
            fontSize: nowSize(40),
            padding: [0, nowSize(20)], // 添加右边距以分隔名称和数值
          },
          unit: {
            color: "#C0CEE5",
            fontSize: nowSize(36), // 可以根据需要调整大小
          },
        },
      },
      formatter: function (name: any) {
        let item: any = props.chartData.find(function (d: any) {
          return d.name === name;
        });
        return `{name|${item.name}}{value|${item.value}}{unit|个}`;
      },
    },
    color: props.color,
    series: [
      {
        type: "pie",
        radius: ["40%", "54%"],
        center: ["50%", "34%"],
        label: {
          show: false,
          //   position: "center",
          textStyle: {
            fontSize: nowSize(40),
            color: "#fff",
          },
        },
        data: props.chartData,
      },
    ],
  };

  option && myChart.setOption(option, true);
};
// 判断是否有数据，有数据渲染echart图
const handleResize = async () => {
  if (myChart) {
    myChart.resize();
    initChart();
  }
};

/**/
watch(
  () => props.chartData,
  async () => {
    if (props.chartData && props.chartData.length) {
      await nextTick();
      initChart();
    }
  },
  { immediate: true },
);
onMounted(async () => {
  window.addEventListener("resize", handleResize);
});
onUnmounted(() => {
  myChart && myChart.dispose();
  window.removeEventListener("resize", handleResize);
});
</script>
<style lang="less" scoped>
.PieChart {
  width: 100%;
  height: 100%;
  .s-empty {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    img {
      width: 513px;
      height: 377px;
    }
  }

  .chart-box {
    position: relative;
    width: 100%;
    height: 100%;

    .chart {
      width: 100%;
      height: 100%;
    }
  }

  .bg-border {
    position: absolute;
    top: 50%;
    left: 21%;
    width: 490px;
    height: 490px;
    border-radius: 50%;
    transform: translateY(-50%);
    box-shadow: 0 0 5px 0 rgba(255, 255, 255, 0.2), 0 0 10px 0 rgba(255, 255, 255, 0.2), 0 0 15px 0px rgba(255, 255, 255, 0.2);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 40px;
    line-height: 49px;
    font-family: MicrosoftYaHei, serif;
    color: #55ffff;
    background: #071c32;
  }
  .title {
    position: absolute;

    width: 240px;
    height: 240px;
    border-radius: 50%;
    top: 34%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 10000;
    font-size: 50px;
    cursor: pointer;
    .text {
      width: 100%;
      height: 120px;
      text-align: center;
      line-height: 160px;
    }
    .num {
      width: 100%;
      height: 120px;
      text-align: center;
      line-height: 80px;
      span {
        color: rgba(255, 119, 45, 0.99);
        font-size: 60px;
      }
    }
  }
}
</style>
